import json

# 3.从非功能视角（例如 API 之间的兼容性，Web API 不同的服务接口协议 REST、RPC），对于相同标注下的 Web API，非功能不同对于参与服务组合的情况统计
print("从非功能视角（例如 API 之间的兼容性，Web API 不同的服务接口协议 REST、RPC），对于相同标注下的 Web API，非功能不同对于参与服务组合的情况统计")
print("数据来自 active_mashups_data.txt、deadpool_mashups_data.txt、active_apis_data.txt、deadpool_apis_data.txt")

mashupTitleList = []
apiServeTypeList = []
apiServerTypeCount = {}
apiUsedCountList = {}
tagContentApi = {}
apiAndStyle = {}

txtFile1 = open(r"../data/raw/api_mashup/active_mashups_data.txt")
txtFile2 = open(r"../data/raw/api_mashup/deadpool_mashups_data.txt")
txtContentList = [txtFile1.read(), txtFile2.read()]

txtFile3 = open(r"../data/raw/api_mashup/active_apis_data.txt")
txtFile4 = open(r"../data/raw/api_mashup/deadpool_apis_data.txt")
txtContentAPIList = [txtFile3.read(), txtFile4.read()]

for txtContent in txtContentList:
    jsondata = json.loads(txtContent)

    for mushup in jsondata:
        if mushup != None:
            mashupTitleList.append(mushup["title"][8:])
            related_apis = mushup["related_apis"]
            apiServeType = []
            for api in related_apis:
                if api != None:
                    title = api["title"]
                    versions = api["versions"]
                    if versions != None and len(versions) > 0:
                        serverType = versions[0]["style"]
                        apiServeType.append((title, serverType))
                        apiAndStyle[title] = serverType
                        if serverType in apiServerTypeCount:
                            apiServerTypeCount[serverType] += 1
                        else:
                            apiServerTypeCount[serverType] = 1
                    if api["title"] in apiUsedCountList:
                        apiUsedCountList[api["title"]] += 1
                    else:
                        apiUsedCountList[api["title"]] = 1
            apiServeTypeList.append(apiServeType)

for txtContent in txtContentAPIList:
    jsondata = json.loads(txtContent)

    for api in jsondata:
        if api != None:
            title = api["title"]
            tags = api["tags"]
            for tag in tags:
                if tag in tagContentApi:
                    tagContentApi[tag].append(title)
                else:
                    tagContentApi[tag] = [title]

print("---------------------------------Mashup 相关 API 使用的服务接口协议---------------------------------")
apiServerCount = zip(mashupTitleList, apiServeTypeList)
for item in apiServerCount:
    print(item)

for key,value in apiServerTypeCount.items():
    print("-----------------------------使用服务接口协议 " + key + " 的 Web API 个数--------------------------------")
    print(key, value)

print("--------------------------------各个 tags 所包含的 API 总数及其中各个 API 被调用次数---------------------------------")
for key,value in tagContentApi.items():
    print("tag：" + key, len(value))
    tempList = []
    for api in value:
        tempList.append((api, apiUsedCountList.get(api, 0), apiAndStyle.get(api)))

    tempList = sorted(tempList, key=lambda x:x[1], reverse=True)
    for item in tempList:
        print(item[0] + " 被调用次数: ", item[1], " 使用的服务接口协议：", item[2])